<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Models\City;
use Illuminate\Support\Facades\Auth;
use App\Repositories\CityRepository;
use App\Http\Requests\Common\City\ShowRequest;
use App\Http\Requests\Common\City\QueryResource;
use App\Http\Requests\Common\City\CityCreate;
use App\Http\Requests\Common\City\CityUpdate;
use App\Http\Requests\Common\City\CityDestroy;

class CityController extends Controller
{
    protected $cityRepository;

    /**
     * ArticlesController constructor.
     * @param CityRepository $cityRepository
     */
    public function __construct(
        CityRepository $cityRepository
    )
    {
        $this->cityRepository = $cityRepository;
    }

    /**
     * 获取城市资源列表
     *
     * Date: 2018/05/10
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $province_id = $request->get('province_id', false);
        $citys = $this->cityRepository->getCity($province_id);
        return success($citys);
    }

    /**
     * 获取城市详情
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, City $city)
    {
        return success($city);
    }


    /**
     * 创建城市
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param CityCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(CityCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $citys = $this->cityRepository->create($attributes);
        return stored($citys);
    }

    /**
     * 更新城市资源
     *
     * Date: 2018/05/11
     * @author Tenny
     * @param CityUpdate $request
     * @param city $city
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(CityUpdate $request, City $city)
    {
        $citysAttr = $request->only([
            'city_name',
            'sort',
        ]);
        $city->update($citysAttr);
        return updated($city);
    }

    /**
     * 删除 城市 资源
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param CityDestroy $request
     * @param City $city
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(CityDestroy $request, City $city)
    {
        $force = $request->get('force');
        $force ? $city->delete() : $city->forceDelete();
        return deleted();
    }
}
